package com.app.qbake.module.square;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.app.qbake.R;
import com.app.qbake.module.square.activity.GoodsDetailActivity;
import com.app.qbake.module.square.event.BannerLoadedEvent;
import com.app.qbake.module.square.layout.SearchCategoryLayout;
import com.wind.base.mvp.view.TabLayoutFragment;
import com.wind.base.utils.ViewInflater;
import com.wind.data.square.SquareRequest;
import com.wind.data.square.bean.BannerItem;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

import java.util.ArrayList;
import java.util.List;

public class SquareTabFragment extends TabLayoutFragment {

    ImageView iv_banner;
    CategoryPopup mCategoryPopup;
    SearchCategoryLayout mSearchCategoryLayout;
    @Override
    public int getLayoutRes() {
        return R.layout.fragment_square_tab;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        EventBus.getDefault().register(this);
        View anchor=view.findViewById(R.id.title_bar);

        iv_banner=view.findViewById(R.id.iv_banner);
        ViewGroup layout_category=view.findViewById(R.id.layout_category);
        mSearchCategoryLayout=new SearchCategoryLayout(getActivity(),layout_category);
       // mCategoryPopup=new CategoryPopup(getActivity(),anchor);
        view.findViewById(R.id.iv_menu).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mSearchCategoryLayout.toggle();
               // toggleCategoryPop();
            }
        });

    }


    private void toggleCategoryPop(){

        mCategoryPopup.toggle();

    }

    @Override
    public List<String> getTitles() {
        List<String> titles=new ArrayList<>();
        titles.add("最新");
        titles.add("热门");
        return titles;
    }

    @Override
    protected List<Fragment> getFragments() {
        List<Fragment> fragments=new ArrayList<>();
        fragments.add(SquareFragment.getInstance(SquareRequest.TYPE_NEW,""));
        fragments.add(SquareFragment.getInstance(SquareRequest.TYPE_HOT,""));
        return fragments;
    }


    @Subscribe
    public void onBannerLoadedEvent(BannerLoadedEvent event){
        final BannerItem bannerItem=event.getBannerList().getBanner().get(0);
        String url=event.getBannerList().getBanner().get(0).getPic_url();
        ViewInflater.inflateImage(iv_banner,url);
        iv_banner.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                GoodsDetailActivity.start(getActivity(),bannerItem.getValue()+"");
            }
        });
    }
    @Override
    public void onDestroyView() {
        super.onDestroyView();
        EventBus.getDefault().unregister(this);
    }
}
